<?php

$image = $_REQUEST['imageid'];

$db_host = "localhost";
$db_port = "3306";
$db_user = "universal";
$db_pass = "4VTqTjZL1Un13K4m";
$db_table = "images.ssmwsab";

$connection = mysqli_connect( $db_host, $db_user, $db_pass, $db_table, $db_port );

/*if( isset( $_REQUEST['c'] ) ) {
	echo "dsfgsdg";
	exit();
}*/

if( mysqli_connect_errno() ) {
	echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

function NotFound( $image ) {
echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\"> 
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /n" . $image . " was not found on this server.</p>
<hr>
<address>Apache/2.4.7 (Ubuntu) Server at images.ssmwsab.rocks Port 443</address>
</body></html>";
exit();
}

$sql = "SELECT * FROM images WHERE filename='" . $image . "' AND confirmed=0";
$result = $connection->query( $sql );
while($row = $result->fetch_assoc()) {
	//echo $_SERVER['REMOTE_ADDR']; exit();
	// if ip address doesnt match, break
	if( !isset( $_REQUEST['c'] ) ) {
	switch( $_SERVER['REMOTE_ADDR'] ) {
		case "172.67.97.168":
		case "5.9.85.215":
		case "101.99.95.48":
		break;
		default:
			if( $row['ipaddress'] != $_SERVER['REMOTE_ADDR'] ) {
				file_put_contents( "testimages.txt", $_SERVER['HTTP_REFERER'] . "\t\t" . $_SERVER[REQUEST_URI] . "\n", FILE_APPEND );
				NotFound( $image );
			}
		break;
	}
	}
	//}
	//if( $_SERVER['REMOTE_ADDR'] != "172.67.97.168" ) {
	//}
	
	// if timestamp greater that 1 hour, break
	
	//print_r( $row );
}

// open the file in a binary mode
$name = 'images/thumbnail/'. $image;
$fp = fopen($name, 'rb');



// send the right headers
header("Content-Type: " . mime_content_type( "images/thumbnail/" . $image ));
header("Content-Length: " . filesize($name));

// dump the picture and stop the script
fpassthru($fp);
exit;
?>